﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace DailyBread.Views
{
    public partial class SharePage : PhoneApplicationPage
    {
        public AppContext Context { get; set; }

        public SharePage()
        {
            InitializeComponent();
            Context = ((App)Application.Current).context;
        }

        private void EmailButton_Click(object sender, RoutedEventArgs e)
        {
            Util.AddToLog("EmailShare");

            EmailComposeTask email = new EmailComposeTask();

            email.Subject = "A good bible verse to share with you - " + Context.VerseName;
            email.Body = String.Concat("I like this verse from Daily Bread. (If you like to read this regularly, install Windows Phone DailyBread app from http://bit.ly/wpdailybread) \n\n", Context.BreadContent);
            email.Show();
        }

        private void SMSButton_Click(object sender, RoutedEventArgs e)
        {
            Util.AddToLog("SMSShare");

            PhoneNumberChooserTask phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
            phoneNumberChooserTask.Show();
        }

        private void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                SmsComposeTask smsComposeTask = new SmsComposeTask();
                smsComposeTask.Body = Context.ArticleVerse + " http://bit.ly/wpdailybread";
                smsComposeTask.To = e.PhoneNumber;
                smsComposeTask.Show();
            }
            else if (e.TaskResult == TaskResult.Cancel)
                MessageBox.Show("Cannot send SMS without phone number", "Number not selected", MessageBoxButton.OK);
            else
                MessageBox.Show("Error getting phone number:\n" + e.Error.Message, "Fail", MessageBoxButton.OK);
        }
    }
}